module.exports = {

	post: {

		'/push/printed': async ctx => {
			const {machine_code, order_id, state, print_time, origin_id, push_time, sign} = ctx.postData
			await ctx.handles.printer.checkElyunSign(push_time, sign)
			const orderId = origin_id
			if(state == 2) {
				const orderData = await ctx.handles.order.getFinishOrderDataByOrderId(orderId)
				const storeData = await ctx.handles.store.getStoreDataByStoreId(orderData.orderStore)
				const printers = await ctx.handles.store.getPrinters(storeData.storeId)
				for(let printer of printers) {
					const printContent = await ctx.handles.order.buildPrintContent({storeName: storeData.storeName}, orderData, printer.templateContent)
					await ctx.handles.printer.addTask('order', printer.printerCode, orderId, printContent)
				}
			}
			else
				await ctx.handles.order.updateOrderPrintStatus(orderId, machine_code, state, print_time)
			return ctx.echo('{"data": "OK"}')
		},

		'/push/printerStatus': async ctx => {
			const {machine_code, online, push_time, sign} = ctx.postData
			await ctx.handles.printer.checkElyunSign(push_time, sign)
			await ctx.handles.printer.updatePrinterStatus(machine_code, online)
			return ctx.echo('{"data": "OK"}')
		},

		'/push/wxPay': async ctx => {
			const {return_code, return_msg, result_code} = ctx.xmlData
			if(return_code == 'SUCCESS' && result_code == 'SUCCESS') {
				const {
					//子商户用户wxId
					sub_openid, 
					//总金额
					total_fee, 
					//微信支付订单号
					transaction_id,
					//商户订单号
					out_trade_no,
					//商户数据包
					attach,
					//支付完成时间
					time_end
				} = ctx.xmlData
				await ctx.handles.order.checkWxPayData(ctx.xmlData)
				const orderId = out_trade_no
				const storeId = attach
				const wxUserId = sub_openid
				const  orderPayTotal = total_fee
				if(!await ctx.handles.order.updateOrderPayStatus({
					orderId,
					storeId,
					//wxUserId: sub_openid,
					wxOrderId: transaction_id,
					//orderPayTotal: total_fee,
					orderPayDate: ctx.common.dateStr2timestamp(time_end)
				})) {
					ctx.common.borderErr('update order pay status failed')
					ctx.common.alertMsg('更新订单支付状态失败', `系统更新 微信支付 的 ${out_trade_no} 订单的支付状态失败`)
					ctx.common.writeLog('error', 'update order pay status failed')
					return ctx.echo('FAIL')
				}
				const {storeName, storeAutoOrder} = await ctx.handles.store.getStoreDataByStoreId(storeId)
				if(storeAutoOrder == 1) {
					const printers = await ctx.handles.store.getPrinters(storeId)
					const orderData = await ctx.handles.order.takeOrder(orderId, printers)
					for(let printer of printers) {
						const printContent = await ctx.handles.order.buildPrintContent({storeName}, orderData, printer.templateContent)
						await ctx.handles.printer.addTask('order', printer.printerCode, orderId, printContent)
					}
					ctx.handles.sys.sendTemplateMsg({
						app: 'gtClient',
						template: 'orderTaked',
						wxUserId,
						page: '/pages/index/index',
						formId: orderData.orderPrePayId,
						data: [orderId, `${storeName} ${orderData.orderTableNum}`, '已成功接单', `${orderPayTotal / 100}元`, ctx.common.timestamp2str(Date.now(), true, true, true, true, true, true)]
					}).catch((err) => {
						ctx.common.alertMsg('模板消息发送失败', `发送给 ${orderData.orderUserName} 的 ${orderId} 订单确认消息未发送成功 ${orderData.orderPrePayId}`, err)
						ctx.common.writeLog('error', `send template msg failed ${orderData.orderPrePayId}`, false, err)
					})
				}
				console.log(ctx.xmlData)
				return ctx.echo('SUCCESS')
			}
			else {
				console.error(return_code, return_msg)
				return ctx.echo('FAIL')
			}
		}

	}

}